﻿package andy.demo.telinfo;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import andy.demo.R;

public class TelInfo extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		

//		System.out.println(getTelInfo());
		
//		TextView  tvinfo = new TextView(this);
//		tvinfo.setText(getTelInfo());
//		setContentView(tvinfo);
		
		
		
		
		
		
		setContentView(R.layout.main);
		showDialog(TelInfo.LOGIN_DIALOG);
		mConnectManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (checkWifiStatus()) {
			dismissDialog(TelInfo.LOGIN_DIALOG);
			return;
		}
		if (!checkGprsStatus()) {
			setNetWorkEnable("enable");
			return;
		}
		dismissDialog(TelInfo.LOGIN_DIALOG);
		
		

	}

	
	private String getTelInfo() {
		
		TelephonyManager tm = (TelephonyManager) this
				.getSystemService(TELEPHONY_SERVICE);
		
		String sdk=android.os.Build.VERSION.SDK;    // SDK号
		String model=android.os.Build.MODEL;   // 手机型号
		String release=android.os.Build.VERSION.RELEASE;  // android系统版本号

		StringBuffer sb = new StringBuffer();
		sb.append("SDK num = " + sdk).append("\n")
			.append("model num =" + model).append("\n")
			.append("release = " + release).append("\n")
			.append("callState =" + tm.getCallState()).append("\n")
			.append("getDeviceId =" + tm.getDeviceId()).append("\n")
			.append("getDeviceSoftwareVersion =" + tm.getDeviceSoftwareVersion()).append("\n")
			.append("getLine1Number =" + tm.getLine1Number()).append("\n")
			.append("getNetworkCountryIso =" + tm.getNetworkCountryIso()).append("\n")
			.append("getNetworkOperator =" + tm.getNetworkOperator()).append("\n")
			.append("getNeighboringCellInfo =" + tm.getNeighboringCellInfo()).append("\n")
			.append("getNetworkType =" + tm.getNetworkType()).append("\n")
			.append("getPhoneType =" + tm.getPhoneType()).append("\n")
			.append("getSimCountryIso =" + tm.getSimCountryIso()).append("\n")
			.append("getSimOperator =" + tm.getSimOperator()).append("\n")
			.append("getSimOperatorName =" + tm.getSimOperatorName()).append("\n")
			.append("getSimSerialNumber =" + tm.getSimSerialNumber()).append("\n")
			.append("getSimState =" + tm.getSimState()).append("\n")
			.append("getSubscriberId =" + tm.getSubscriberId()).append("\n")
			.append("getVoiceMailAlphaTag =" + tm.getVoiceMailAlphaTag()).append("\n")
			.append("getVoiceMailNumber =" + tm.getVoiceMailNumber()).append("\n")
			.append("hasIccCard =" + tm.hasIccCard()).append("\n")
			.append("isNetworkRoaming =" + tm.isNetworkRoaming()).append("\n");
			
		return "" + sb;
		
	}
	
	
	
		public final static int LOGIN_DIALOG = 1;
		private ConnectivityManager mConnectManager;
		/** The open gprs time counter we remember. */
		private int num = 0;
		private ProgressDialog mDialog;

		/** Open the gprs. */
		public void setNetWorkEnable(String cmd) {

			String[] args = new String[3];
			args[0] = "svc";
			args[1] = "data";
			args[2] = cmd;

			try {
				Process process = Runtime.getRuntime().exec(args);

				// get the err line
				InputStream stderr = process.getErrorStream();
				InputStreamReader isrerr = new InputStreamReader(stderr);
				BufferedReader brerr = new BufferedReader(isrerr);

				// get the output line
				InputStream outs = process.getInputStream();
				InputStreamReader isrout = new InputStreamReader(outs);
				BufferedReader brout = new BufferedReader(isrout);

				String line = null;
				String result = "";

				// get the whole error message string
				while ((line = brerr.readLine()) != null) {
					result += line;
					result += "\n";
				}

				if (result != "") {
					// put the result string on the screen
					System.out.println("the error outcome is ___" + result);
				}

				result = "";
				// get the whole standard output string
				while ((line = brout.readLine()) != null) {
					result += line;
					result += "\n";
				}
				if (result != "") {
					// put the result string on the screen
					System.out.println("the outcome is ___" + result);
				}
				if (!cmd.equalsIgnoreCase("disable")) {
					try {
						Thread.sleep(2000);
						checkState();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} catch (Throwable t) {
				t.printStackTrace();
			}
		}

		/** Check the wifi is open or not. */
		public boolean checkWifiStatus() {
			return mConnectManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
					.getState() == NetworkInfo.State.CONNECTED ? true : false;
		}

		/** Check the Gprs is open or not. */
		public boolean checkGprsStatus() {
			return mConnectManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
					.getState() == NetworkInfo.State.CONNECTED ? true : false;
		}

		protected Dialog onCreateDialog(int id) {
			switch (id) {
			case TelInfo.LOGIN_DIALOG:
				mDialog = new ProgressDialog(TelInfo.this);
				mDialog.setMessage("GPRS开启中....");
				return mDialog;
			default:
				return null;
			}
		}

		public boolean onKeyDown(int keyCode, KeyEvent event) {
			switch (keyCode) {
			case 4:
				if (checkGprsStatus()) {
					setNetWorkEnable("disable");
				}
				finish();
				break;
			}
			return true;
		}

		/** Check the gprs is opened or not,if not try to open one time again. */
		public void checkState() {
			num++;
			if (!checkGprsStatus() && num < 2) {
				setNetWorkEnable("enable");
			} else {
				dismissDialog(TelInfo.LOGIN_DIALOG);
			}
		}
	
}